//
//  LHAlertView.m
//  WXYView
//
//  Created by lehuo on 2017/1/10.
//  Copyright © 2017年 lehuo. All rights reserved.
//

#import "LHAlertView.h"
typedef enum
{
    OptionStyle,
    ShowStyle
}AlertViewStyle;//标示弹窗类型
//屏幕宽高
#define kSceenW [UIScreen mainScreen].bounds.size.width
#define kSceenH [UIScreen mainScreen].bounds.size.height
#define BackgroundColor [UIColor colorWithRed:129/255.0 green:129/255.0 blue:129/255.0 alpha:0.5]

#define AlertViewSpace 30 //弹窗距离屏幕左右边的距离
#define bodySpace 15 //内容与弹窗左右的距离
#define AlertWidth kSceenW - 2 * AlertViewSpace
#define TitleFont [UIFont boldSystemFontOfSize:17]
#define TitleFontColor [UIColor blackColor]
#define BodyFont [UIFont systemFontOfSize:15]
#define BodyFontColor [UIColor lightGrayColor]

#define btnHeight 30
#define bottomSpace 15
#define btnBackgroundColor [UIColor colorWithRed:214/255.0 green:88/255.0  blue:69/255.0  alpha:1.0]
#define BtnFont [UIFont systemFontOfSize:15]

@interface LHAlertView()
@property(nonatomic,assign)float height;//窗口的高度
@property(nonatomic,strong)NSString *title;//标题
@property(nonatomic,strong)NSString *body;//内容
@property(nonatomic,strong)NSString *sureTitle;//确定标题
@property(nonatomic,strong)NSString *cancelTitle;//取消标题
@property(nonatomic,strong)NSString *closeTitle;//关闭回调
@property(nonatomic,strong)PopSelectIndex popSelectIndex;//选项回调
@property(nonatomic,strong)PopClose popClose;//关闭窗口回调
@property(nonatomic,assign)AlertViewStyle style;//弹窗的类型
@property(nonatomic,strong)UIView *bgVeiw;//弹窗
@end
@implementation LHAlertView

+ (void)showAlertViewWith:(NSString *)title body:(NSString *)body cancelTitle:(NSString *)cancelTitle sureTitle:(NSString *)sureTitle block:(PopSelectIndex)popSelectIndex
{
    
    LHAlertView *alertView = [[LHAlertView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    alertView.backgroundColor = BackgroundColor;
    alertView.sureTitle = sureTitle;
    alertView.title = title;
    alertView.cancelTitle = cancelTitle;
    alertView.popSelectIndex = popSelectIndex;
    alertView.body = body;
    alertView.style = OptionStyle;
    [alertView show];
}

+ (void)showAlertViewWith:(NSString *)title body:(NSString *)body closeTitle:(NSString *)closeTitle block:(PopClose)popClose
{
    LHAlertView *alertView = [[LHAlertView alloc] init];
    alertView.title = title;
    alertView.closeTitle = closeTitle;
    alertView.popClose = popClose;
    alertView.body = body;
    alertView.style = ShowStyle;
    [alertView show];
}

#pragma mark - 添加控件
- (void)show
{
//    [LHCoverView show];
    UIView *bgView = [[UIView alloc] init];
    [self addSubview:bgView];
    bgView.backgroundColor = [UIColor whiteColor];
    self.bgVeiw = bgView;
    
    
    float originY =  15;
    float titleH = 20;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, originY, AlertWidth, titleH)];
    titleLabel.text = self.title;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = TitleFont;
    titleLabel.textColor = TitleFontColor;
    [bgView addSubview:titleLabel];
    originY = originY + titleH;
    UILabel *bodyLabel = [[UILabel alloc] init];
    CGSize size = [self.body boundingRectWithSize:CGSizeMake(AlertWidth - 2 * bodySpace, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:BodyFont} context:nil].size;
    bodyLabel.textColor = BodyFontColor;
    bodyLabel.text = self.body;
    bodyLabel.font = BodyFont;
    bodyLabel.numberOfLines = 0;
    float space = 10;
    bodyLabel.frame = CGRectMake(bodySpace, originY + space, AlertWidth - 2 * bodySpace, size.height);
    [bgView addSubview:bodyLabel];
    originY = originY + space * 2 + size.height;
    if (self.style == OptionStyle) [self showOptionAlertView:originY];
    else [self showAlertView:originY];
    
    originY = originY + btnHeight + bottomSpace;
    self.height = self.height + btnHeight + bottomSpace;
    //错误的frame用于动画
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = BackgroundColor;
//     self.bgVeiw.frame = CGRectMake(AlertViewSpace, kSceenH * 0.5 - originY * 0.5, AlertWidth, originY);
//    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
//        self.bgVeiw.frame = CGRectMake(AlertViewSpace, kSceenH * 0.5 - originY * 0.5, AlertWidth, originY);
//    } completion:nil];
    self.bgVeiw.frame = CGRectMake(AlertViewSpace, kSceenH * 0.5 - originY * 0.5, AlertWidth, originY);
//    self.bgVeiw.center = self.center;
    self.bgVeiw.transform = CGAffineTransformScale(self.bgVeiw.transform, 0.1, 0.1);
    [UIView animateWithDuration:0.3 animations:^{
        self.bgVeiw.transform = CGAffineTransformScale(self.bgVeiw.transform, 11, 11);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.2 animations:^{
            self.bgVeiw.transform =CGAffineTransformIdentity;
        }];
    }];
}

#pragma mark - 显示只有确认取消按钮的AlertView
- (void)showOptionAlertView:(float)originY
{
    UIButton *cancelBtn = [[UIButton alloc] init];
    [cancelBtn setTitle:self.cancelTitle forState:UIControlStateNormal];
    cancelBtn.backgroundColor = [UIColor grayColor];
    float space = 30;
    float btnWidth = (AlertWidth - 2 * bodySpace - space) * 0.5 ;
    cancelBtn.frame = CGRectMake(bodySpace, originY, btnWidth, btnHeight);
    cancelBtn.tag = 0;
    cancelBtn.titleLabel.font = BtnFont;
    [cancelBtn addTarget:self action:@selector(optionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    cancelBtn.layer.cornerRadius = cancelBtn.frame.size.height * 0.4;
    cancelBtn.layer.masksToBounds = YES;
    [self.bgVeiw addSubview:cancelBtn];
    
    UIButton *sureBtn = [[UIButton alloc] init];
    [sureBtn setTitle:self.sureTitle forState:UIControlStateNormal];
    sureBtn.backgroundColor = btnBackgroundColor;
    sureBtn.frame = CGRectMake(AlertWidth - bodySpace - btnWidth, originY, btnWidth, btnHeight);
    sureBtn.tag = 1;
    sureBtn.titleLabel.font = BtnFont;
    sureBtn.layer.cornerRadius = sureBtn.frame.size.height * 0.4;
    sureBtn.layer.masksToBounds = YES;
    [sureBtn addTarget:self action:@selector(optionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.height = originY;
    [self.bgVeiw addSubview:sureBtn];
    
    
    
}

#pragma mark - 显示只有一个按钮的AlertView
- (void)showAlertView:(float)originY
{
    UIButton *closeBtn = [[UIButton alloc] init];
    [closeBtn setTitle:self.closeTitle forState:UIControlStateNormal];
    closeBtn.backgroundColor = btnBackgroundColor;
    closeBtn.frame = CGRectMake(bodySpace, originY, AlertWidth - 2 * bodySpace, btnHeight);
    closeBtn.layer.cornerRadius = closeBtn.frame.size.height * 0.4;
    closeBtn.layer.masksToBounds = YES;
    closeBtn.titleLabel.font = BtnFont;
    [closeBtn addTarget:self action:@selector(closeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.height = originY;
    [self.bgVeiw addSubview:closeBtn];
}

#pragma mark - 关闭按钮点击事件
- (void)optionBtnClick:(UIButton *)btn
{
    if (self.popSelectIndex)
    {
        self.popSelectIndex(btn.tag);
    }
    [self dissmiss];
}

#pragma mark - 关闭按钮点击事件
- (void)closeBtnClick
{
    if (self.popClose)
    {
        self.popClose();
    }
    [self dissmiss];
}

#pragma mark - 移除AlertView
- (void)dissmiss
{
    [UIView animateWithDuration:0.5 animations:^{
//        _bgVeiw.frame = CGRectMake(AlertViewSpace, -kSceenH, AlertWidth, self.bgVeiw.height);
        self.bgVeiw.transform = CGAffineTransformScale(self.bgVeiw.transform, 0.001, 0.001);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - 显示一段简单信息

+ (void)showSimpleInfo:(NSString *)content
{
    UIButton *bgBtn = [[UIButton alloc] init];
    CGSize size = [content boundingRectWithSize:CGSizeMake(AlertWidth - 3 * AlertViewSpace, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:BodyFont} context:nil].size;
    [bgBtn setTitle:content forState:UIControlStateNormal];
    [bgBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    bgBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    bgBtn.titleLabel.numberOfLines = 0;
    float bgBtnH = size.height + AlertViewSpace;
    bgBtn.frame = CGRectMake(AlertViewSpace, kSceenH * 0.5 - bgBtnH * 0.5, kSceenW - 2 * AlertViewSpace, bgBtnH);
    bgBtn.contentEdgeInsets = UIEdgeInsetsMake(AlertViewSpace * 0.5, AlertViewSpace * 0.5, AlertViewSpace * 0.5, AlertViewSpace * 0.5);
    bgBtn.backgroundColor = [UIColor colorWithRed:30/255.0 green:30/255.0 blue:30/255.0 alpha:0.8];
    bgBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    bgBtn.layer.cornerRadius = bgBtnH * 0.4;
    bgBtn.layer.masksToBounds = YES;
    [[UIApplication sharedApplication].keyWindow addSubview:bgBtn];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [bgBtn removeFromSuperview];
    });
}
@end
